<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/11/17
 * Time: 12:29 PM
 */

namespace App\Library\User;


use App\Models\User;

class AchievementManager
{
    /**
     * 增加节点业绩
     * @param User $user
     * @param $amount
     */
    public static function addNodeAchievement(User $user, $amount)
    {
        $wallet = $user->wallet()->firstOrCreate([]);
        $wallet->node_achievement+= $amount;
        $wallet->save();

        $archievement = $user->Achievements()->where('datestamp', date('Ymd'))->firstOrCreate(['datestamp'=>date('Ymd')]);
        $archievement->achievement = $wallet->node_achievement;
        $archievement->node_achievement+= $amount;
        $archievement->save();

        $relation = $user->relation;
        if ($relation)
        {
            $upuser = $relation->upuser;
            if ($upuser)
            {
                AchievementManager::addNodeAchievement($upuser, $amount);
            }
        }
    }

    public static function addNodeCommission(User $user, $amount)
    {
        $wallet = $user->wallet()->firstOrCreate([]);
        $wallet->node_commission+= $amount;
        $wallet->save();

        $archievement = $user->Achievements()->where('datestamp', date('Ymd'))->firstOrCreate(['datestamp'=>date('Ymd')]);
        $archievement->commission = $wallet->node_commission;
        $archievement->node_commission+= $amount;
        $archievement->save();

        $relation = $user->relation;
        if ($relation)
        {
            $upuser = $relation->upuser;
            if ($upuser)
            {
                AchievementManager::addNodeCommission($upuser, $amount);
            }
        }
    }
}
